<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工位id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationId">
              <!--mes_base_factory,factory_name,id-->
              <j-dict-select-tag type="list" v-model="model.stationId" :dictCode="stationDict"
                                 placeholder="请选择工位id"
                                 :useCache="false"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标准节拍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standardBeat">
              <a-input v-model="model.standardBeat" placeholder="请输入标准节拍"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工位属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stationAttribute">
              <j-dict-select-tag type="list" v-model="model.stationAttribute" dictCode="station_attribute"
                                 placeholder="请选择工位属性"
                                 @change="chooseStationAttribute"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.stationAttribute==='2' ||model.stationAttribute==='3' ">
            <a-form-model-item label="检测类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkType">
              <j-dict-select-tag type="list" v-model="model.checkType" dictCode="check_type"
                                 placeholder="请选择检测类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工序序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="procedureSerialNo">
              <a-input-number v-model="model.procedureSerialNo" placeholder="请输入工序序号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesTechnologyProcedureForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        stationDict: 'mes_base_factory,factory_name,id,factory_level=4 and factory_status=1',
        model: {
          technologyId: ''
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          stationId: [
            { required: true, message: '请选择工位id!' }
          ],
          standardBeat: [
            { required: true, message: '请输入标准节拍!' }
          ],
          stationAttribute: [
            { required: true, message: '请选择工位属性!' }
          ],
          checkType: [
            { required: true, message: '请选择检测类型!' }
          ],
          procedureSerialNo: [
            { required: true, message: '请输入工序序号!'},
            { pattern: /^[1-9]\d*$/, message: '请输入整数!'},
          ],
        },
        url: {
          add: '/mes/mesTechnologyProcedure/add',
          edit: '/mes/mesTechnologyProcedure/edit',
          queryById: '/mes/mesTechnologyProcedure/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
    },
    methods: {
      chooseStationAttribute(){
        if (this.model.stationAttribute==='1'){
          this.model.checkType='';
        }
      },
      add(technologyId, lineId) {
        //给model赋值
        this.model.technologyId = technologyId
        this.modelDefault = JSON.parse(JSON.stringify(this.model))
        //工位重新取值
        this.stationDict = 'mes_base_factory,factory_name,id,factory_level=4 and factory_status=1 and pid=\'' + lineId + '\''

        this.edit(this.modelDefault)
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.visible = true
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      }
    }
  }
</script>